package cn.itsource.org.controller;

import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopLog;
import cn.itsource.org.domain.vo.ShopVo;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.ExcelUtil;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController // 等价于  @Controller+@ResponseBody
@RequestMapping("/shop")
public class ShopController {


    @Autowired
    private IShopService shopService;



    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        System.out.println(shop);

        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }

    }

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId()!=null){
                shopService.update(shop);
            }else {
                shopService.save(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

    /*
        "/{id}"  在url里面有一个动态的占位符 名字id
        @PathVariable("id") 获取url里面的名字叫做id的占位符数据
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }

    }

    // @RequestMapping("/loadById")
    @GetMapping("/{id}")
    public Shop loadById(@PathVariable("id") Long id){
        try {
            return  shopService.loadById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //@RequestMapping(value = "/loadAll",method = RequestMethod.GET)
    @GetMapping
    public List<Shop> loadAll(){
        try {
            return  shopService.loadAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    //定义分页的方法
    @PostMapping
    public PageResult<Shop> queryPage(@RequestBody ShopQuery query){
        System.out.println(query);
        return shopService.queryPage(query);
    }


    /*
        前台发送的数据：[10,12,14]

        后台接收数据：
            1.数组 Long[] ids
            2.集合
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopLog shopLog){
        try {
            shopService.reject(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   /* @GetMapping("/importExcel")
    public void importExcel( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "role", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
    @GetMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        //保存到数据库中

    }


    @GetMapping("/echarts")
    public List<Integer> echartsData(){
        List<ShopVo> shopVos=shopService.countByState();
        //把其中的每月数据转换为list或者数组  前端用于图表的y轴数据
        List<Integer> counts = shopVos.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        return counts;
    }


}
